package com.sw.chinesewriteboard.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.SeekBar;

/**
 * @Description:
 * @author: GordenGao
 * @Email: gordengao124@gmail.com
 * @date: 2021年06月22日 11:41
 */
public class MulticolorSeekBar extends SeekBar {

    /**
     * 画笔
     */
    private Paint mMulticlourPaint;
    /**
     * 刻度线的个数,等分数等于刻度线的个数加1
     */
    private int mMulticlourCount = 3;
    /**
     * 每条刻度线的宽度
     */
    private int mMulticlourWidth = 2;
    /**
     * 刻度线的颜色
     */
    private int mMulticlourColor = Color.WHITE;
    /**
     * 滑块上面是否要显示刻度线
     */
    private boolean isShowTopOfThumb = false;

    public MulticolorSeekBar(Context context) {
        super(context);
        init();
    }

    public MulticolorSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MulticolorSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    /**
     * 初始化
     */
    private void init() {
//创建绘制刻度线的画笔
        mMulticlourPaint = new Paint();
        mMulticlourPaint.setColor(mMulticlourColor);
        mMulticlourPaint.setAntiAlias(true);
//Api21及以上调用，去掉滑块后面的背景
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
            setSplitTrack(false);
        }
    }

    /**
     * 重写onDraw方法绘制刻度线
     *
     * @param canvas
     */
    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//极限条件校验
        if (getWidth() <= 0 || mMulticlourCount <= 0) {
            return;
        }
//获取每一份的长度
// int length = (getWidth() - getPaddingLeft() - getPaddingRight() - mMulticlourCount * mMulticlourWidth) / (mMulticlourCount + 1);
        int length = (getWidth() - getPaddingLeft() - getPaddingRight()) / (mMulticlourCount + 1);
//计算刻度线的顶部坐标和底部坐标
        int rulerTop = getHeight() / 2 - getMinimumHeight() / 2;
        int rulerBottom = rulerTop + getMinimumHeight();
//获取滑块的位置信息
        Rect thumbRect = null;
        if (getThumb() != null) {
            thumbRect = getThumb().getBounds();
        }
//绘制刻度线
// for (int i = 1; i <= mMulticlourCount; i++) {
//  //计算刻度线的左边坐标和右边坐标
//  int rulerLeft = i * length + getPaddingLeft();
//  int rulerRight = rulerLeft + mMulticlourWidth;
//
//  //判断是否需要绘制刻度线
//  if (!isShowTopOfThumb && thumbRect != null && rulerLeft - getPaddingLeft()   thumbRect.left && rulerRight - getPaddingLeft() < thumbRect.right) {
//  continue;
//  }
//
//  //进行绘制
//  canvas.drawRect(rulerLeft, rulerTop, rulerRight, rulerBottom, mMulticlourPaint);
// }
        for (int i = 0; i <= mMulticlourCount; i++) {
            int left = getPaddingLeft() + i * length;
            int right = left + length;
            if (i % 3 == 0) {
                mMulticlourPaint.setColor(Color.LTGRAY);
            } else if (i % 3 == 1) {
                mMulticlourPaint.setColor(Color.GRAY);
            } else {
                mMulticlourPaint.setColor(Color.DKGRAY);
            }
            if (i == 0) {
                canvas.drawCircle(getPaddingLeft() + 10, 20, 10, mMulticlourPaint);
                left += 10;
                canvas.drawRect(left, 10, right, 30, mMulticlourPaint);
            } else if (i == mMulticlourCount) {
                right -= 10;
                canvas.drawRect(left, 10, right, 30, mMulticlourPaint);
                canvas.drawCircle(right, 20, 10, mMulticlourPaint);
            } else {
                canvas.drawRect(left, 10, right, 30, mMulticlourPaint);
            }
        }
    }

    /**
     * 设置刻度线的个数
     *
     * @param mRulerCount
     */
    public void setRulerCount(int mRulerCount) {
        this.mMulticlourCount = mRulerCount;
        requestLayout();
    }

    /**
     * 设置刻度线的宽度，单位(px)
     *
     * @param mRulerWidth
     */
    public void setRulerWidth(int mRulerWidth) {
        this.mMulticlourWidth = mRulerWidth;
        requestLayout();
    }

    /**
     * 设置刻度线的颜色
     *
     * @param mRulerColor
     */
    public void setRulerColor(int mRulerColor) {
        this.mMulticlourColor = mRulerColor;
        if (mMulticlourPaint != null) {
            mMulticlourPaint.setColor(mRulerColor);
            requestLayout();
        }
    }

    /**
     * 滑块上面是否需要显示刻度线
     *
     * @param isShowTopOfThumb
     */
    public void setShowTopOfThumb(boolean isShowTopOfThumb) {
        this.isShowTopOfThumb = isShowTopOfThumb;
        requestLayout();
    }

}
